<template>
  <transition-group
    class="drag-sort-box"
    tag="div"
    @dragover="dragover($event)"
  >
    <div
      v-for="(item, index) in mockData"
      :key="index"
      :class="['drag-sort-item']"
      :draggable="true"
      @dragstart="dragstart(item, index)"
      @dragenter="dragenter(item, $event)"
      @dragend="dragend()"
      @dragover="dragover($event)"
    >
      <span :class="index == dargIndex ? 'active' : ''">{{ item.name }}</span>
    </div>
  </transition-group>
</template>

<script lang="ts" setup>
import { ref } from "vue";
import { DiagramHeaderModel } from "./DiagramHeaderModel";

const mockData = ref([
  {
    name: "string1"
  },
  {
    name: "string2"
  },
  {
    name: "string3"
  },
  {
    name: "string4"
  },
  {
    name: "string5"
  },
  {
    name: "string6"
  }
]);
let dargIndex = ref(-1);
let oldData = null;
let newData = null;

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const props = defineProps<{
  diagramHeaderModel: DiagramHeaderModel;
}>();

const dragstart = (value, index) => {
  oldData = value;
  dargIndex.value = index;
};
const dragenter = (value, e) => {
  newData = value;
  e.preventDefault();
};

const dragover = e => {
  e.preventDefault();
};

const dragend = () => {
  if (oldData !== newData) {
    let oldIndex = mockData.value.indexOf(oldData);
    let newIndex = mockData.value.indexOf(newData);

    let newItems = [...mockData.value];
    newItems.splice(oldIndex, 1);
    newItems.splice(newIndex, 0, oldData);
    mockData.value = [...newItems];
  }
  dargIndex.value = -1;
};
</script>
<style lang="scss">
.drag-sort-box {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
}
.drag-sort-box .drag-sort-item {
  width: 100px;
  height: 100px;
  margin: 2px;
  cursor: pointer;
  transition: all 0.3s;
  background: #ccc;
  position: relative;
}

.drag-sort-box .drag-sort-item .active {
  border-radius: 30px;
  box-shadow: 0 0 15px rgba(0, 0, 0, 0.3);
  opacity: 0;
}
</style>
